package com.ttg.common.utils;

/**
 * @author zhangHongYuan
 * @description
 * @date 2024/3/21 13:38
 */
public class QxAndJhUtil {


    public static String get8VaildDate(String date) {

        String newDate = "";

        if (date.length() == 5) {

            newDate = date.substring(0, 4) + "0" + date.substring(4) + "01";

        } else if (date.length() == 6) {

            newDate = date + "01";

        } else if (date.length() == 7) {

            if ("1".equals(date.substring(4, 5))) {

                newDate = date.substring(0, 6) + "0" + date.substring(6);

            } else {

                newDate = date.substring(0, 4) + "0" + date.substring(4);

            }

        } else if (date.length() == 8) {

            newDate = date;

        } else {

            newDate = date.substring(0, 8);

        }

        return newDate;

    }

    /**

     * 获取符合标准日期(YYYYMMDD)

     *

     * @param date

     * @return

     */

    public static String getYYYYMMDDDate(String date) {

        String result = date;

        String dateType = date.trim();

// 对带有符号的处理("/",".","-")

        if (dateType.contains("/") || dateType.contains(".") || dateType.contains("-")) {

            String midd2 = "";

            String back = dateType.substring(0, 4);

            if (dateType.contains("/")) {

                midd2 = dateType.replace("/", "-");

            }

            if (dateType.contains(".")) {

                midd2 = dateType.replace(".", "-");

            }

            if (dateType.contains("-")) {

                midd2 = dateType.replace("-", "-");

            }

            if (dateType.length() < 8) {

                int midd = Integer.valueOf(midd2.substring(midd2.lastIndexOf("-") + 1));

                if (midd < 10) {

                    back += "0" + String.valueOf(midd);

                } else {

                    back += String.valueOf(midd);

                }

            }

            if (dateType.length() >= 8) {

                int midd = Integer.valueOf(midd2.substring(midd2.indexOf("-") + 1, midd2.lastIndexOf("-")));

                if (midd < 10) {

                    back += "0" + String.valueOf(midd);

                } else {

                    back += String.valueOf(midd);

                }

                int midd1 = Integer.valueOf(midd2.substring(midd2.lastIndexOf("-") + 1));

                if (midd1 < 10) {

                    back += "0" + String.valueOf(midd1);

                } else {

                    back += String.valueOf(midd1);

                }

            }

            result = back;

        }

// 对带有年、月、日的处理

        if (dateType.contains("年")) {

            String temp = "";

            temp += dateType.substring(0, 4);

            if (dateType.contains("月")) {

                int temp1 = Integer.valueOf(dateType.substring(dateType.indexOf("年") + 1, dateType.indexOf("月")));

                if (temp1 < 10) {

                    temp += "0" + String.valueOf(temp1);

                } else {

                    temp += temp1;

                }

            }

            if (dateType.contains("日")) {

                int temp2 = Integer.valueOf(dateType.substring(dateType.indexOf("月") + 1, dateType.indexOf("日")));

                if (temp2 < 10) {

                    temp += "0" + String.valueOf(temp2);

                } else {

                    temp += temp2;

                }

            }

            result = temp;

        }

        return QxAndJhUtil.get8VaildDate(result);
    }
}
